﻿using System;
using System.Data;
using System.Collections.Generic;

using el.Model;
using el.DAL;
using System.Text;
using System.Data.SqlClient;

namespace el.BLL
{
    /// <summary>
    /// Sys_Admin
    /// </summary>
    public partial class Sys_Admin
    {
        private readonly el.DAL.Sys_Admin dal = new el.DAL.Sys_Admin();
        public Sys_Admin()
        { }

        public string GetUserAccess(int UserID)
        {
            string sql = "select Access from dbo.Sys_Role where RoleID=(select RoleID from dbo.Sys_Admin where UID=" + UserID.ToString() + ")";

            DataSet dt = DbHelperSQL.Query(sql);

            return dt.Tables[0].Rows[0][0].ToString();
        }

        

        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录




        /// </summary>
        public bool Exists(int UID)
        {
            return dal.Exists(UID);
        }

        /// <summary>
        /// 登入
        /// </summary>
        /// <param name="kls_uid"></param>
        /// <param name="kls_pwd"></param>
        /// <param name="kls_num"></param>
        /// <returns></returns>
        public string Existslongin(string uname, string upwd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM [Sys_Admin]");
            strSql.Append(" where UserName=@UserName and Password=@Password");
            SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
                    new SqlParameter("@Password", SqlDbType.VarChar,250)};
            parameters[0].Value = uname;
            parameters[1].Value = upwd;


            //return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            SqlDataReader dr = DbHelperSQL.ExecuteReader(strSql.ToString(), parameters);
            string uid = "";
            if (dr.Read())
            {
                uid = dr["Uid"].ToString();
            }
            return uid;
        }


        /// <summary>
        /// 增加一条数据




        /// </summary>
        public int Add(el.Model.Sys_Admin model)
        {
            model.CreateDate = DateTime.Now;
            model.IsDelete = 0;
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据




        /// </summary>
        public bool Update(el.Model.Sys_Admin model)
        {
            model.LastUpdateDate = DateTime.Now;
            model.IsDelete = 0;
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据




        /// </summary>
        public bool Delete(int UID)
        {

            return dal.Delete(UID);
        }
        /// <summary>
        /// 删除一条数据




        /// </summary>
        public bool DeleteList(string UIDlist)
        {
            return dal.DeleteList(UIDlist);
        }


        /// <summary>
        /// 删除一条数据




        /// </summary>
        public int DeleteLogic(int ID, int userID)
        {
            string sql = "update dbo.Sys_Admin set IsDelete=1,LastUpdateBy=" + userID.ToString() + ", LastUpdateDate=getdate() where UID=" + ID.ToString();
            return DbHelperSQL.ExecuteSql(sql);
        }

        /// <summary>
        /// GetAll
        /// </summary>
        public DataTable GetAllView()
        {
            string sql = "select * from dbo.V_Sys_Admin where [IsDelete]=0";
            return DbHelperSQL.Query(sql).Tables[0];
        }

        /// <summary>
        /// 得到一个对象实体




        /// </summary>
        public el.Model.Sys_Admin GetModel(int UID)
        {

            return dal.GetModel(UID);
        }

        ///// <summary>
        ///// 得到一个对象实体，从缓存中
        ///// </summary>
        //public el.Model.Sys_Admin GetModelByCache(int UID)
        //{

        //    string CacheKey = "Sys_AdminModel-" + UID;
        //    object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
        //    if (objModel == null)
        //    {
        //        try
        //        {
        //            objModel = dal.GetModel(UID);
        //            if (objModel != null)
        //            {
        //                int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
        //                Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
        //            }
        //        }
        //        catch { }
        //    }
        //    return (el.Model.Sys_Admin)objModel;
        //}

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据




        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<el.Model.Sys_Admin> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<el.Model.Sys_Admin> DataTableToList(DataTable dt)
        {
            List<el.Model.Sys_Admin> modelList = new List<el.Model.Sys_Admin>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                el.Model.Sys_Admin model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new el.Model.Sys_Admin();
                    if (dt.Rows[n]["UID"] != null && dt.Rows[n]["UID"].ToString() != "")
                    {
                        model.UID = int.Parse(dt.Rows[n]["UID"].ToString());
                    }
                    if (dt.Rows[n]["RoleID"] != null && dt.Rows[n]["RoleID"].ToString() != "")
                    {
                        model.RoleID = int.Parse(dt.Rows[n]["RoleID"].ToString());
                    }
                    if (dt.Rows[n]["UserName"] != null && dt.Rows[n]["UserName"].ToString() != "")
                    {
                        model.UserName = dt.Rows[n]["UserName"].ToString();
                    }
                    if (dt.Rows[n]["Password"] != null && dt.Rows[n]["Password"].ToString() != "")
                    {
                        model.Password = dt.Rows[n]["Password"].ToString();
                    }
                    if (dt.Rows[n]["Email"] != null && dt.Rows[n]["Email"].ToString() != "")
                    {
                        model.Email = dt.Rows[n]["Email"].ToString();
                    }
                    if (dt.Rows[n]["Mobile"] != null && dt.Rows[n]["Mobile"].ToString() != "")
                    {
                        model.Mobile = dt.Rows[n]["Mobile"].ToString();
                    }
                    if (dt.Rows[n]["CreateBy"] != null && dt.Rows[n]["CreateBy"].ToString() != "")
                    {
                        model.CreateBy = int.Parse(dt.Rows[n]["CreateBy"].ToString());
                    }
                    if (dt.Rows[n]["CreateDate"] != null && dt.Rows[n]["CreateDate"].ToString() != "")
                    {
                        model.CreateDate = DateTime.Parse(dt.Rows[n]["CreateDate"].ToString());
                    }
                    if (dt.Rows[n]["LastUpdateBy"] != null && dt.Rows[n]["LastUpdateBy"].ToString() != "")
                    {
                        model.LastUpdateBy = int.Parse(dt.Rows[n]["LastUpdateBy"].ToString());
                    }
                    if (dt.Rows[n]["LastUpdateDate"] != null && dt.Rows[n]["LastUpdateDate"].ToString() != "")
                    {
                        model.LastUpdateDate = DateTime.Parse(dt.Rows[n]["LastUpdateDate"].ToString());
                    }
                    if (dt.Rows[n]["IsDelete"] != null && dt.Rows[n]["IsDelete"].ToString() != "")
                    {
                        model.IsDelete = int.Parse(dt.Rows[n]["IsDelete"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("IsDelete=0");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

